package org.helsoft.mojo.api;

import java.io.File;
import java.io.FilenameFilter;

public class BundleFileFilter implements FilenameFilter {

	private final Bundle bundle;
	private final boolean anyVersion;
		
	/**
	 * 
	 * @param aBundle
	 * @param anyVersion
	 */
	public BundleFileFilter(Bundle aBundle, boolean anyVersion) {
		this.bundle = aBundle;
		this.anyVersion = anyVersion;
	}
	
	/**
	 * 
	 * @param aBundle
	 */
	public BundleFileFilter(Bundle aBundle) {
		this(aBundle, false);
	}
	
	@Override
	public boolean accept(File dir, String name) {
		boolean result = false;
		if (bundle != null) {
			if (bundle.getVersions().isEmpty() || anyVersion) {
				result = name.startsWith(bundle.getFullName() + "_");
			} else if (!bundle.getVersions().isEmpty()) {
				for (Version version : bundle.getVersions()) {
					result = name.startsWith(bundle.getFullName() + "_" + version);
					if (result) {
						break;
					}
				}
			}
		}
		return result;
	}

}
